package com.un.ebs.businessObject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.un.ebs.core.util.AESTool;
import com.un.ebs.sys.domain.DataSource;
import com.un.ebs.sys.domain.Field;
import com.un.ebs.sys.mapper.DataSourceMapper;
import com.un.ebs.businessObject.service.DataFieldService;
import com.un.ebs.sys.uitl.PoolUtils;
import com.un.ebs.sys.uitl.SqlCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表操作业务实现类
 *
 * @Author lcl
 * @Date 2021/01/20 15:07:22
 */
@Service
@Slf4j
public class DataFieldServiceImpl implements DataFieldService {

    @Autowired
    private DataSourceMapper dataSourceMapper;

    @Override
    public boolean createTable(List<Field> list, String tableName, String name) {
        DataSource ds = getDataSource(name);
        String sql = SqlCodeUtil.createTableSql(list, tableName, ds.getLibraryName(), ds.getDbType());
        log.info("创建表语句：" + sql);
        return PoolUtils.executeSql(sql, ds);

    }

    @Override
    public boolean dropTable(String tableName, String name) {
        DataSource ds = getDataSource(name);
        String sql = SqlCodeUtil.delTableSql(tableName, ds.getLibraryName(), ds.getDbType());
        log.info("删除表语句：" + sql);
        return PoolUtils.executeSql(sql, ds);
    }

    @Override
    public boolean addField(Field field, String tableName, String name) {
        DataSource ds = getDataSource(name);
        List<String> sql = SqlCodeUtil.addFieldSql(field, tableName, ds.getLibraryName(), ds.getDbType());
        log.info("添加表字段语句：" + sql);
        return PoolUtils.executeSqlList(sql, ds);
    }

    @Override
    public boolean delField(Field field, String tableName, String name) {
        DataSource ds = getDataSource(name);
        List<String> sql = SqlCodeUtil.delFieldSql(field, tableName, ds.getLibraryName(), ds.getDbType());
        log.info("删除表字段语句：" + sql);
        return PoolUtils.executeSqlList(sql, ds);
    }

    @Override
    public boolean updateField(Field field, String tableName, String oldFieldName, String name) {
        DataSource ds = getDataSource(name);
        List<String> sql = SqlCodeUtil.updateFieldSql(field, tableName, oldFieldName, ds);
        log.info("修改表字段语句：" + sql);
        return PoolUtils.executeSqlList(sql, ds);
    }

    @Override
    public boolean isExistTable(String tableName, String name) {
        DataSource ds = getDataSource(name);
        String sql = SqlCodeUtil.isExistTable(ds.getLibraryName(), tableName, ds.getDbType());
        log.info("判断表是否存在语句：" + sql);
        return PoolUtils.isExistSql(sql, ds);
    }

    //获取DbMethod
    public DataSource getDataSource(String name) {
        DataSource ds = dataSourceMapper.selectOne(new QueryWrapper<DataSource>().eq("name", name));
        //ds.setPassword(AESTool.Decrypt(ds.getPassword()));
        //ds.setIpAddr(AESTool.Decrypt(ds.getIpAddr()));
        ds.setPassword(ds.getPassword());
        ds.setIpAddr(ds.getIpAddr());
        return ds;
    }
}
